<?php
/**
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Member extends Common
{
    public function index()
    {
        $ks = input('ks');
        $html = Db::name('member')->order('open desc , userid desc')->where('username|userid', 'like', '%' . $ks . '%')->paginate(15, false, $config = ['query' => array('ks' => $ks)]);
        $this->assign('html', $html);
        return view();
    }
    public function edit()
    {
        $id = input('id');
        $this->assign('id', $id);
        $c = Db::name('member')->where('userid', $id)->find();
        $this->assign('c', $c);
        return view();
    }
    //系统消息
    public function xx()
    {
        return view();
    }
    //积分记录
    public function jifen()
    {
        $ks = input('ks');
        $html = Db::name('point_note')->order('id desc')->where('uid', 'like', '%' . $ks . '%')->paginate(15, false, $config = ['query' => array('ks' => $ks)]);
        $this->assign('html', $html);
        return view();
    }
    //会员组
    public function zu()
    {
        $html = Db::name('member_zu')->order('id desc')->paginate(15);
        $this->assign('html', $html);
        return view();
    }
    //添加
    public function insert()
    {
        $data = $this->request->post();
        $password = input('password');
        $key = 'www.115cms.com';
        $data['regtime'] = time();
        $data['usermail'] = input('usermail');
        $data['userip'] = $_SERVER['REMOTE_ADDR'];
        $data['userhead'] = '/public/img/touxiang.jpg';
        $data['password'] = jiami($password, 'ENCODE', $key, 0);
        $html = Db::name('member');
        $mail = $html->where('usermail', $data['usermail'])->find();
        if (!$mail) {
            if (!is_email($data['usermail'])) {
                $this->error('邮箱格式错误');
            }
            if (!$html->data($data)->insert()) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功');
            }
        } else {
            return json(array('code' => 0, 'msg' => '该邮箱已存在'));
        }
    }
    //添加权限组
    public function insertzu()
    {
        $data = $this->request->post();
        $qx = $_POST['qx'];
        $data['qx'] = implode(',', $qx);
        if (!Db::name('memberZu')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //添加消息
    public function insertxx()
    {
        $data = $this->request->post();
        $data['tid'] = 0;
        $data['uid'] = 0;
        $data['userid'] = input('userid');
        $data['content'] = xss(input('content'));
        $data['open'] = 1;
        $data['model'] = 0;
        $data['time'] = time();
        if (!Db::name('xiaoxi')->data($data)->insert()) {
            $this->error('发送失败');
        } else {
            $this->success('发送成功');
        }
    }
    //更新权限组
    public function updatezu()
    {
        $id = input('id');
        $data = $this->request->post();
        if (!empty($_POST['qx'])) {
           $data['qx'] = implode(',', $_POST['qx']);
        } else {
           $data['qx'] = '';
        }
        if (Db::name('memberZu')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //更新
    public function update()
    {
        $id = input('userid');
        $this->assign('id', $id);
        $data = $this->request->post();
        $key = 'www.115cms.com';
        $data['userid'] = $id;
        $mima = fun_sc_p('member', array('userid' => $id), 'password');
        if (empty(input('password'))) {
            $data['password'] = $mima;
        } else {
            $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        }
        if (!Db::name('member')->data($data)->update()) {
            $this->error('修改失败');
        } else {
            $this->success('修改成功');
        }
    }
    //删除
    public function delete()
    {
        $id = input('id');
        $m = input('m');
        $data = $this->request->get();
        if (Db::name($m)->where('userid', $id)->data($data)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新
    public function update_index()
    {
        $id = input('id');
        $or = input('or');
        $m = input('m');
        $data = $this->request->post();
        $s = Db::name($m)->where('userid', $id)->find();
        if ($s[$or] == 1) {
            Db::name($m)->where('userid', $id)->update([$or => 0]);
            return json(array('code' => 200, 'msg' => '更新成功', 'or' => 1));
        } else {
            Db::name($m)->where('userid', $id)->update([$or => 1]);
            return json(array('code' => 200, 'msg' => '更新成功', 'or' => 2));
        }
    }
}